#include "rng.h"
#include "delay.h"

uint8_t RNG_Init(void)
{
	uint16_t Count = 0;
	RCC->AHB2ENR |= (0x1<<6);//打开RNGCLK_EN
	
	RNG->CR |= (0x1<<2);//开启RNGEN
	RNG->CR |= (0x1<<+3);//开启RNGIE   开了但是没有用  相当于错误导致中断不予理睬	
	while(!(RNG->SR & RNG_FLAG_DRDY))
	{
		Count++;
		delay_us(100);              //如果一直就绪不了发生超时返回失败
		if(Count>10000)
		{
			return 0;//失败
		}
	}
	return 1;	
}

uint32_t Get_RandomNumber(void)
{
  while(!(RNG->SR & RNG_FLAG_DRDY));  //检查随机数数据是否就绪   就绪可以拿数据
  return RNG->DR;
}


int32_t Get_RandomRange(int min,int max)
{
  return (int32_t)(min + Get_RandomNumber()%(max - min + 1));  //封装一个介于min 到max 之间的函数
}

void HASH_RNG_IRQHandler(void)
{
	
}	
